package com.moneywelcome.investassembly.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.moneywelcome.goldfacade.GoldQueryApi;
import com.moneywelcome.http.ServletUtil;
import com.moneywelcome.investfacade.InvestRedeemApi;
import com.moneywelcome.investfacade.dto.DividendRecordDto;
import com.moneywelcome.investfacade.dto.RedeemDto;
import com.moneywelcome.investfacade.dto.RedeemRecordDto;
import com.moneywelcome.investfacade.dto.SubscribeDto;
import com.moneywelcome.investfacade.vo.RecordPageVo;
import com.moneywelcome.investfacade.vo.RedeemVo;
import com.moneywelcome.orderfacade.FinancialApi;
import com.moneywelcome.orderfacade.OrderCreatApi;
import com.moneywelcome.orderfacade.OrderQueryApi;
import com.moneywelcome.orderfacade.dto.RedeemOrderDto;
import com.moneywelcome.orderfacade.dto.UserFinancialDto;
import com.moneywelcome.orderfacade.vo.OrderPageVo;
import com.moneywelcome.orderfacade.vo.RedeemRecordVo;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.Result;
import com.moneywelcome.validate.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@Slf4j
@Api(tags = "用户投资赎回服务")
public class InvestRedeemApiImpl implements InvestRedeemApi {
    @Autowired
    private OrderCreatApi ocApi;
    @Autowired
    private OrderQueryApi oqApi;
    @Autowired
    private GoldQueryApi gqApi;
    @Autowired
    private FinancialApi financialApi;
//    @Resource
//    private UserApi userApi;
    @ApiOperation("黄金赎回")
    @Override
    public Result<RedeemDto>redeem(RedeemVo redeemVo) {
        log.info("{}",redeemVo);
        ValidationUtils.validate(redeemVo);
        //获取用户id
        Integer userId = Integer.valueOf(ServletUtil.getHttpServletRequest().getHeader("userId"));
        RedeemRecordVo redeemRecordVo = BeanUtil.copyProperties(redeemVo, RedeemRecordVo.class);
        //用户信息查询校验
//        UserInfoDto userInfoDto = userApi.selUserinfo(userId).getData();
//        redeemRecordVo.setNickName(userInfoDto.getNickname());
        redeemRecordVo.setUserId(userId);
        UserFinancialDto data = financialApi.queryById(redeemVo.getFinancialId()).getData();
        //查询赎回费率
        redeemRecordVo.setRedeemRate(gqApi.queryRedeemRule(data.getGoldId()).getData().getRedeemRate());
        return Result.success(BeanUtil.copyProperties(ocApi.redeemOrder(redeemRecordVo).getData(),RedeemDto.class));
    }
    @ApiOperation("分页查询赎回记录")
    @Override
    public Result<PageResult<RedeemRecordDto>> redeemRecord(RecordPageVo recordPageVo) {
        ValidationUtils.validate(recordPageVo);
        OrderPageVo orderPageVo = BeanUtil.copyProperties(recordPageVo, OrderPageVo.class);
        Integer userId = Integer.valueOf(ServletUtil.getHttpServletRequest().getHeader("userId"));
        orderPageVo.setUserId(userId);
        PageResult<RedeemOrderDto> data = oqApi.redeemRecord(orderPageVo).getData();
        PageResult<RedeemRecordDto> pageResult = BeanUtil.copyProperties(data, PageResult.class);
        pageResult.setData(BeanUtil.copyToList(data.getData(),RedeemRecordDto.class));
        return Result.success(pageResult);
    }
    @Deprecated
    @ApiOperation("赎回记录查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "redeemSerial",value = "赎回编号")
    )
    @Override
    public Result<RedeemRecordDto> redeemInfo(String redeemSerial) {
        return null;
    }

}
